package com.ruoyi.supermarket.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.enums.AuditStatusType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.ProductResMapper;
import com.ruoyi.supermarket.domain.ProductRes;
import com.ruoyi.supermarket.service.ProductResService;

/**
 * 商品资源Service业务层处理
 * 
 * @author liqiwei
 * @date 2023-11-15
 */
@Service
public class ProductResServiceImpl implements ProductResService
{
    @Autowired
    private ProductResMapper productResMapper;

    /**
     * 查询商品资源
     * 
     * @param id 商品资源主键
     * @return 商品资源
     */
    @Override
    public ProductRes selectProductResById(Long id)
    {
        return productResMapper.selectProductResById(id);
    }

    /**
     * 查询商品资源列表
     * 
     * @param productRes 商品资源
     * @return 商品资源
     */
    @Override
    public List<ProductRes> selectProductResList(ProductRes productRes)
    {
        return productResMapper.selectProductResList(productRes);
    }

    /**
     * 新增商品资源
     * 
     * @param productRes 商品资源
     * @return 结果
     */
    @Override
    public int insertProductRes(ProductRes productRes)
    {
        String file = productRes.getFile();
        String uuid = IdUtils.fastSimpleUUID();
        productRes.setUuid(uuid);
        productRes.setFile(file);
        productRes.setAuditStatus(AuditStatusType.COMMIT.getStatus());
        productRes.setAuditRemark("");
        productRes.setDeleteStatus(0);
        productRes.setUpdateTime(new Date());
        return productResMapper.insertProductRes(productRes);
    }

    /**
     * 修改商品资源
     * 
     * @param productRes 商品资源
     * @return 结果
     */
    @Override
    public int updateProductRes(ProductRes productRes)
    {
        productRes.setUpdateTime(DateUtils.getNowDate());
        return productResMapper.updateProductRes(productRes);
    }

    /**
     * 批量删除商品资源
     * 
     * @param ids 需要删除的商品资源主键
     * @return 结果
     */
    @Override
    public int deleteProductResByIds(Long[] ids)
    {
        return productResMapper.deleteProductResByIds(ids);
    }

    /**
     * 提交商品资源
     *
     * @param pr 商品资源
     * @return 结果
     */
    @Override
    public int commitProductRes(ProductRes pr) {
        ProductRes productRes = new ProductRes();
        productRes.setUuid(pr.getUuid());
        List<ProductRes> productResList = productResMapper.selectProductResList(productRes);

        productRes = productResList.get(0);
        //如果是未提交，这更改为待审核
        if(AuditStatusType.COMMIT.getStatus().equals(productRes.getAuditStatus())){
            productRes.setAuditStatus(AuditStatusType.AUDIT.getStatus());
            productRes.setAuditRemark("");
            productRes.setUpdateTime(new Date());
            return productResMapper.updateProductRes(productRes);
        }
        return 0;
    }

    /**
     * 审核商品资源
     *
     * @param pr 商品资源
     * @return 结果
     */
    @Override
    public int auditProductRes(ProductRes pr) {
        ProductRes productRes = new ProductRes();
        productRes.setUuid(pr.getUuid());
        List<ProductRes> productResList = productResMapper.selectProductResList(productRes);

        productRes = productResList.get(0);
        productRes.setAuditStatus(pr.getAuditStatus());
        productRes.setAuditRemark(pr.getAuditRemark());
        productRes.setUpdateTime(new Date());
        return productResMapper.updateProductRes(productRes);
    }

    /**
     * 删除商品资源信息
     * 
     * @param id 商品资源主键
     * @return 结果
     */
    @Override
    public int deleteProductResById(Long id)
    {
        return productResMapper.deleteProductResById(id);
    }
}
